.TH std::type_info::operator==,std::type_info::operator!= 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::type_info::operator==,std::type_info::operator!= \- std::type_info::operator==,std::type_info::operator!=

.SH Synopsis
   bool operator==( const type_info& rhs ) const;           \fI(until C++11)\fP
   bool operator==( const type_info& rhs ) const            \fI(since C++11)\fP
   noexcept;                                                (until C++23)
   constexpr bool operator==( const type_info& rhs  \fB(1)\fP     (since C++23)
   ) const noexcept;
   bool operator!=( const type_info& rhs ) const;                         \fI(until C++11)\fP
   bool operator!=( const type_info& rhs ) const        \fB(2)\fP               \fI(since C++11)\fP
   noexcept;                                                              \fI(until C++20)\fP

   Checks if the objects refer to the same types.

   The != operator is synthesized from operator==. \fI(since C++20)\fP

.SH Parameters

   rhs - another type information object to compare to

.SH Return value

   true if the comparison operation holds true, false otherwise.

.SH Notes

        Feature-test macro       Value    Std                   Feature
   __cpp_lib_constexpr_typeinfo 202106L (C++23) Constexpr for
                                                std::type_info::operator==

.SH Example


// Run this code

 #include <iostream>
 #include <string>
 #include <typeinfo>
 #include <utility>

 class person
 {
 public:
     explicit person(std::string n) : name_(std::move(n)) {}
     virtual const std::string& name() const { return name_; }

 private:
     std::string name_;
 };

 class employee : public person
 {
 public:
     employee(std::string n, std::string p)
         : person(std::move(n)), profession_(std::move(p)) {}

     const std::string& profession() const { return profession_; }

 private:
     std::string profession_;
 };

 void print_info(const person& p)
 {
     if (typeid(person) == typeid(p))
         std::cout << p.name() << " is not an employee\\n";
     else if (typeid(employee) == typeid(p))
     {
         std::cout << p.name() << " is an employee ";
         auto& emp = dynamic_cast<const employee&>(p);
         std::cout << "who works in " << emp.profession() << '\\n';
     }
 }

 int main()
 {
     print_info(employee{"Paul","Economics"});
     print_info(person{"Kate"});

 #if __cpp_lib_constexpr_typeinfo
     if constexpr (typeid(employee) != typeid(person)) // C++23
         std::cout << "class `employee` != class `person`\\n";
 #endif
 }

.SH Possible output:

 Paul is an employee who works in Economics
 Kate is not an employee
 class `employee` != class `person`

.SH See also

          checks whether the referred type precedes referred type of another type_info
   before object in the implementation defined order, i.e. orders the referred types
          \fI(public member function)\fP
